﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using System.Collections.Generic;

namespace TankIt
{
    public class Tank
    {
        Texture2D texture;

        
        float scale = 1f;
        public Vector2 position;
        public Vector2 speed;
        Vector2 center;
        public float rotation;
        int life;
        public BoundingSphere bb;
        BoundingSphere bbOld;
        public bool andaFrente = false;
        public bool andaTras = false;
        bool sptopped = true;
        public int heat;
        

        Engine gameEngine;
        
        //Microsoft.Xna.Framework.Color cor;

        ContentManager content;

        public Tank(float scale, ContentManager content, Vector2 position,float rotation,Engine eng)
        {
            this.scale = scale;
            this.content = content;
            this.position = position;
            this.rotation=rotation;
            life = 100;
            speed = Vector2.Zero;
            gameEngine = eng;
            heat = 0;
            
        }

        public Vector2 getPosition()
        {
            return new Vector2(position.X, position.Y);
        }

        public float getRotation()
        {
            return rotation;
        }

        public void load()
        {
            texture = content.Load<Texture2D>("Tank");

            center = new Vector2(texture.Width / 2, texture.Height / 2);

            bb = new BoundingSphere(new Vector3(position.X, position.Y, 0), texture.Height / 2);

        }


        public void draw(SpriteBatch batch)
        {
            batch.Draw(texture, position, null, Microsoft.Xna.Framework.Color.White, rotation, center, scale, SpriteEffects.None, 0f);
        }

        public void takeDamage(int damage){
            life-=damage;
        }

        public void rotate(float angle, GameTimerEventArgs gametime)
        {
            
            //bbOld = bb;
            //float rotationOld = rotation;
            rotation +=angle;

            
            //float oldx,oldy;


            //oldx = speed.X;
            //oldy = speed.Y;

            //speed.X = oldx * (float)Math.Cos(angle) - oldy * (float)Math.Sin(angle);
            //speed.Y = oldy * (float)Math.Cos(angle) + oldx * (float)Math.Sin(angle);

            ////bb = gameEngine.actualizarBoundingBox(position, texture, rotation);

            //if (gameEngine.testColisions(bb))
            //{
            //    rotationOld = rotation;

            //    bb = bbOld;

            //}

            
        }


        public void calculaVelocidade(GameTimerEventArgs gametime)
        {
           float oldx,oldy;

           if (andaTras == true || andaFrente == true)
           {

               oldx = 50;
               oldy = 0;

               speed.X = oldx * (float)Math.Cos(rotation) - oldy * (float)Math.Sin(rotation);
               speed.Y = oldy * (float)Math.Cos(rotation) + oldx * (float)Math.Sin(rotation);

               if (andaFrente == true) speed *= -1;
              
           }
           else speed = Vector2.Zero;
               


        }

        public void Update(GameTimerEventArgs e,bool frente,bool tras)
        {
            if (texture == null) return;
            andaFrente = frente;
            andaTras=tras;
            //if (gameEngine.testColisions(bb)) return;
            
            Vector2 positionOld=new Vector2(position.X,position.Y);

            calculaVelocidade(e);
            
            position += speed * (float)e.ElapsedTime.TotalSeconds;

            bbOld = bb;

            bb.Center.X = position.X;
            bb.Center.Y = position.Y;

            if (gameEngine.testColisions(bb))
            {
                position.X = positionOld.X;
                position.Y = positionOld.Y;

                bb = bbOld;

            }

        }

        public void Update(GameTimerEventArgs e,Unmarshled un)
        {
            position = un.position;

            bb.Center.X = position.X;
            bb.Center.Y = position.Y;

            rotation = un.rotation;
        }

    }

}
